package com.chenl.provider.DEMOS.EasyExcel.demo;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.metadata.data.ClientAnchorData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.util.FileUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
 
/**
 * 图片导出
 *
 * @author dqh
 * @date 2022/11/3 15:20
 */
public class ImageExportTest {
 
    /**
     * 图片导出
     * <p>
     * 1. 创建excel对应的实体对象 参照{@link ImageData}
     * <p>
     * 2. 直接写即可
     */
    @Test
    public void imageWrite() throws Exception {
        //文件最后生成的路径
        String fileName = "D:\\data\\" + "imageWrite.xlsx";
        System.out.println(fileName);
 
        //测试用的图片路径
        String imagePath = "D:\\data\\1.png";
        String imagePath2 = "D:\\data\\1.png";
 
        //数据列表Demo
        List<ImageData> demoDataList = new ArrayList<>();
 
        //图片路径
        List<String> path1 = new ArrayList<>();
        path1.add(imagePath);
 
        List<String> path2 = new ArrayList<>();
        path2.add(imagePath);
        path2.add(imagePath2);
 
        List<String> path3 = new ArrayList<>();
        path3.add(imagePath);
        path3.add(imagePath);
        path3.add(imagePath2);
 
        List<String> path4 = new ArrayList<>();
        path4.add(imagePath);
        path4.add(imagePath2);
        path4.add(imagePath);
        path4.add(imagePath2);
 
//        demoDataList.add(new ImageData("王离", path1));
//        demoDataList.add(new ImageData("杨开", path2));
//        demoDataList.add(new ImageData("欧阳微微", path3));
//        demoDataList.add(new ImageData("上官婉儿", path4));
        genImageExcel(demoDataList, fileName);
 
    }


 
    /**
     * 生成有图片的excel
     *
     * @param demoDataList 数据列表
     * @param fileName     生成文件路径
     */
    private void genImageExcel(List<ImageData> demoDataList, String fileName) {
        if (CollectionUtils.isEmpty(demoDataList)) {
            return;
        }
        //图片列最大图片数
        AtomicReference<Integer> maxImageSize = new AtomicReference<>(0);
        demoDataList.forEach(item -> {
            if (CollectionUtils.isNotEmpty(item.getImagePathList()) && item.getImagePathList().size() > maxImageSize.get()) {
                maxImageSize.set(item.getImagePathList().size());
            }
        });
        //设置列长度所用类
        com.chenl.provider.DEMOS.EasyExcel.demo.AutoColumnWidthStyleStrategy longWidth = new com.chenl.provider.DEMOS.EasyExcel.demo.AutoColumnWidthStyleStrategy();
        demoDataList.forEach(item -> {
            WriteCellData<Void> writeCellData = new WriteCellData<>();
            if (CollectionUtils.isNotEmpty(item.getImagePathList())) {
                //每张图片间距
                Integer splitWidth = 2;
                //每张图片的长度
                Integer imageWidth = 80;
                //图片列的最大长度
                Integer sumWidth = maxImageSize.get() * (imageWidth + splitWidth);
                List<com.alibaba.excel.metadata.data.ImageData> imageDataList = new ArrayList<>();
                List<String> imagePathList = item.getImagePathList();
                for (int i = 1; i <= imagePathList.size(); i++) {
                    String path = imagePathList.get(i - 1);
                    Integer left = imageWidth * (i - 1) + i * splitWidth;
                    Integer right = sumWidth - imageWidth - left;
                    com.alibaba.excel.metadata.data.ImageData imageData = new com.alibaba.excel.metadata.data.ImageData();
                    try {
                        imageData.setImage(FileUtils.readFileToByteArray(new File(path)));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    imageData.setImageType(com.alibaba.excel.metadata.data.ImageData.ImageType.PICTURE_TYPE_PNG);
                    //距离单元格顶部距离
                    imageData.setTop(1);
                    //距离单元格底部距离
                    imageData.setBottom(1);
                    //距离单元格左边距离
                    imageData.setLeft(left);
                    //距离单元格右边距离
                    imageData.setRight(right);
                    imageData.setAnchorType(ClientAnchorData.AnchorType.DONT_MOVE_DO_RESIZE);
                    imageDataList.add(imageData);
                }
                writeCellData.setImageDataList(imageDataList);
                Map<String, Integer> zdyColumnWidth = new HashMap<>();
                //图片列名称，对应导出对象的列名称，图片列长度
                zdyColumnWidth.put("图片信息", sumWidth / 6);
                longWidth.setZdyColumnWidth(zdyColumnWidth);
            }
            item.setWriteCellDataFile(writeCellData);
        });
        //写入数据
        EasyExcel.write(fileName, ImageData.class).registerWriteHandler(longWidth).sheet().doWrite(demoDataList);
    }
 
 
}